home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / TURBOPASCAL WIN / OWLDEMOS.PAK / OLEAPP.PAS < prev    next >
Pascal/Delphi Source File  |  1992-06-08  |  7KB  |  240 lines

  1. {***************************************************}
  2. {                                                   }
  3. {   Turbo Pascal for Windows                        }
  4. {   Windows 3.1 OLE Server Demonstration Program    }
  5. {               Application Unit                    }
  6. {                                                   }
  7. {   Copyright (c) 1992 by Borland International     }
  8. {                                                   }
  9. {***************************************************}
  10.  
  11. unit OLEApp;
  12.  
  13. { This unit contains the definition of the OLE Server
  14.   Application Object.
  15.  
  16.   Note that this application object is defined in its own
  17.   unit because other objects in the program need to reference
  18.   their owning application.
  19.  
  20.   Note: To compile the OLE Server demo, set Compile|Primary File to OLESERVR.PAS
  21. }
  22.  
  23. interface
  24.  
  25. uses Ole, WObjects, Server;
  26.  
  27. type
  28.  
  29. { Application Object }
  30.  
  31.   POLEApp  = ^TOLEApp;
  32.   TOLEApp  = object(TApplication)
  33.     Server       : POleServerObj;
  34.     cfNative     : TOleClipFormat;
  35.     cfOwnerLink  : TOleClipFormat;
  36.     cfObjectLink : TOleClipFormat;
  37.  
  38.     procedure InitInstance; virtual;
  39.     procedure CreateServer; virtual;
  40.     procedure Wait(var WaitFlag: Boolean); virtual;
  41.     function  RegisterClipboardFormats: Boolean; virtual;
  42.     Procedure Error(ErrorCode: Integer); virtual;
  43.   end;
  44.  
  45. implementation
  46.  
  47. uses WinTypes, WinProcs, OleTypes, Strings,
  48.      ServrWin, OleObj;
  49.  
  50.  
  51. { TOleApp Methods }
  52.  
  53. { Processes the command line and check for option /Embedding or -Embedding,
  54.   then create the OLE server. There are four scenarios we are concerned with:
  55.  
  56.     1. Case One: oleservr.exe
  57.       - Embedding = False; create an untitled document
  58.  
  59.     2. Case two: oleservr.exe filename
  60.       - Embedding = False; create a new document from the file
  61.  
  62.     3. Case three: oleservr.exe -Embedding
  63.       - Embedding = True; do NOT create or register a document.
  64.                           do NOT show a window until client requests it
  65.  
  66.     4. Case four: oleservr.exe -Embedding filename
  67.       - Embedding = True; load file, register it (this is the linking case)
  68.                           do NOT show a window until client requests it
  69. }
  70. procedure TOleApp.CreateServer;
  71. var
  72.   Strng    : PChar;
  73.   Embedded : Boolean;
  74.   Path     : PChar;
  75.   ServerObj: POleServerObj;
  76. begin
  77.   Strng    := CmdLine;
  78.   Embedded := False;
  79.   Path     := nil;
  80.  
  81.   { Skip any whitespace
  82.   }
  83.   if Strng <> nil then
  84.   begin
  85.     while (Strng^ = ' ') and (Strng^ <> #0) do
  86.       inc(Strng);
  87.  
  88.   { Check for a '-' or '/'.  If found, check for the "Embedding"
  89.     option.  Then, skip past the option to the file name.
  90.   }
  91.     if (Strng^ = '-') or (Strng^ = '/') then
  92.     begin
  93.       Embedded := (StrIComp(@Strng[1], Embedding) <> 0);
  94.       while (Strng^ <> ' ') and (Strng^ <> #0) do
  95.         inc(Strng);
  96.     end;
  97.  
  98.   { Skip any whitespace before looking for the file name
  99.   }
  100.     while (Strng^ = ' ') and (Strng^ <> #0) do
  101.       inc(Strng);
  102.  
  103.     if Strng^ <> #0 then
  104.       Path := Strng;
  105.   end
  106.   else
  107.   begin
  108.     Embedded := False;
  109.     Path     := nil;
  110.   end;
  111.  
  112.   { If we are embedded, then we won't display the window until requested
  113.     to by the library.
  114.   }
  115.   if Embedded then
  116.     CmdShow := sw_Hide;
  117.  
  118.   { Create the server object.  Recall that the object will attach itself
  119.     to this application, much as a child window attaches to a parent, so
  120.     we don't need to hold the results of these New's.
  121.   }
  122.   if Path <> nil then
  123.     New(ServerObj, InitFromFile(@Self, Path))
  124.   else
  125.     New(ServerObj, Init(@Self, Embedded));
  126. end;
  127.  
  128. { Registers the clipboard formats.  If you are a mini-server (embedding 
  129.   only) you will need to register clipboard formats for "Native" and 
  130.   "OwnerLink".  If you are a full server (linking and embedding) you will
  131.   also need to register clipboard format "ObjectLink"
  132. }
  133. function TOleApp.RegisterClipboardFormats: Boolean;
  134. begin
  135.   cfNative    := RegisterClipboardFormat('Native');
  136.   cfOwnerLink := RegisterClipboardFormat('OwnerLink');
  137.   cfObjectLink:= RegisterClipboardFormat('ObjectLink');
  138.  
  139.   RegisterClipboardFormats :=    (cfNative     <> 0)
  140.                              and (cfOwnerLink  <> 0)
  141.                              and (cfObjectLink <> 0);
  142. end;
  143.  
  144. { Initializes this instance of the OLE application, by doing the following:
  145.     - Create the main window
  146.     - Create OLE VTbl thunks
  147.     - Create clipboard formats
  148.     - Parse the command line
  149.     - Create/register OLE server
  150.  
  151.   NOTE: We let Windows free all thunks when the application terminates,
  152.         and don't do it ourselves
  153. }
  154. procedure TOleApp.InitInstance;
  155. begin
  156.   MainWindow := New(PServerWindow, Init(nil, DemoTitle));
  157.   MainWindow := MakeWindow(MainWindow);
  158.  
  159.   RegisterType(ROleObjectObj);
  160.  
  161.   if (not TOleServerObj_InitVTbl(HInstance) or
  162.       not TOleDocument_InitVTbl(HInstance) or
  163.       not TOleObjectObj_InitVTbl(HInstance)
  164.      )
  165.   then
  166.     Status := olInitVTblError
  167.   else 
  168.     if not RegisterClipboardFormats then
  169.       Status := olRegClipError
  170.     else
  171.       CreateServer;
  172.  
  173.   { We do this *after* calling CreateServer, because if we are embedded
  174.     then we don't want to display the main window until requested to by
  175.     the server library, and it is CreateServer who determines that and sets
  176.     'CmdShow' accordingly
  177.   }
  178.   if MainWindow <> nil then
  179.     MainWindow^.Show(CmdShow)
  180.   else
  181.     Status := em_InvalidMainWindow;
  182. end;
  183.  
  184. { Redefines the Error method to trap error messages generated by OLE app,
  185.   display an error message box and terminate the application.
  186. }
  187. procedure TOleApp.Error(ErrorCode: Integer);
  188. var
  189.   Strng : PChar;
  190. begin
  191.   Strng := nil;
  192.   if (ErrorCode = olRegClipError) then
  193.     Strng := 'Fatal Error: Cannot register ''Native'', ''OwnerLink'', and ' +
  194.       '''ObjectLink'' clipboard formats'
  195.   else 
  196.     if (ErrorCode = olInitVTBLError) then
  197.       Strng := 'Fatal Error: Cannot create thunks for ''OleServer'', ' +
  198.         '''OleServerDoc'', and ''OleObject'' VTbls';
  199.  
  200.   if Strng <> nil then
  201.   begin
  202.     MessageBox(0, Strng, DemoTitle, mb_OK or mb_IconStop);
  203.     PostAppMessage(GetCurrentTask, wm_Quit, 0, 0);
  204.   end
  205.   else
  206.     TApplication.Error(ErrorCode);
  207. end;
  208.  
  209. { Dispatches messages until the given flag is set to True.  One use of this
  210.   function is to wait until a Release method is called after a function has
  211.   returned Ole_Wait_for_Release.
  212.  
  213.   PARAMETER: "WaitFlag" is a reference to a flag that will be set to True
  214.              when we can return.
  215. }
  216. procedure TOleApp.Wait(var WaitFlag: Boolean);
  217. var
  218.   Msg         :  TMsg;
  219.   MoreMessages:  Bool;
  220. begin
  221.   MoreMessages := False;
  222.   while not WaitFlag do
  223.   begin
  224.     OleUnblockServer(Server^.ServerHdl, MoreMessages);
  225.  
  226.     if not MoreMessages then 
  227.     begin
  228.       { If there are no more messages in the OLE queue, go to system queue
  229.       }
  230.       if (GetMessage(Msg, 0, 0, 0)) then
  231.       begin
  232.         TranslateMessage(Msg);
  233.         DispatchMessage (Msg);
  234.       end;
  235.     end;
  236.   end;
  237. end;
  238.  
  239. end.
  240.